import {Component, OnInit} from "@angular/core";
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {UploadChangeParam, UploadFile} from 'ng-zorro-antd/upload';
import {NzMessageService} from "ng-zorro-antd";
import {ActivatedRoute, Router} from "@angular/router";
import {FetchService} from "../../../core/net/fetch.service";

@Component({
  selector: 'app-project-files-detail',
  templateUrl: './project-files-detail.component.html',
  styleUrls: ['./project-files-detail.component.scss']
})
export class ProjectFilesDetailComponent implements OnInit {
  projectId;
  form: FormGroup;
  yearOption = [
    {value: '', label: '全部'},
    {value: '2019', label: '2019'},
    {value: '2020', label: '2020'},
  ];
  typeOption = [
    {value: '', label: '全部'},
    {value: '1', label: '类型一'},
    {value: '2', label: '类型二'},
  ];
  tableData;
  fileUploadForm: FormGroup;
  nzBodyStyle: any = {
    padding: '16px 20px 0'
  };
  fileList: UploadFile[] = [];
  uploadUrl = `${(window as any).environment.apiBaseUrl}comm/file/upload`;
  fileModalTitle = '文件上传';
  fileModalShow = false;
  uploadFileId = '';

  constructor(private fb: FormBuilder,
              private message: NzMessageService,
              private fetchService: FetchService,
              private routerInfo: ActivatedRoute,
              private router: Router
  ) {
    this.form = this.fb.group({
      id: [null],
      name: [null],
      year: [null],
      manager: [null],
      telephone: [null],
      type: [null],
      districtId: [null],
      address: [null],
      remark: [null]
    });

    this.fileUploadForm = this.fb.group({
      projectId: [null],
      fileName: [null],
      fileTypeName: [null],
      num: [null],
      version: [null]
    });
    this.projectId = this.routerInfo.snapshot.queryParams['id'];
  }

  ngOnInit(): void {
    if (this.projectId) {
      this.checkDetail();
    }
  }

  checkDetail() {
    this.fetchService.get<any>(`archives/wrsProjectInfo/${this.projectId}`).subscribe(res => {
      /* this.tableData = [
         {id: '1', name: '前期工作文件及批复文件'},
         {id: '2', name: '主管部门批文'},
         {id: '3', name: '招标投标文件'},
         {id: '4', name: '合同文件'},
         {id: '5', name: '工程项目划分资料'},
         {id: '6', name: '单元工程质量评定资料'},
         {id: '7', name: '分部工程质量评定资料'},
         {id: '8', name: '单位工程质量评定资料'},
         {id: '9', name: '工程外观质量评定资料'},
         {id: '10', name: '工程质量管理有关文件'},
         {id: '11', name: '工程安全管理有关文件'},
         {id: '12', name: '工程施工质量检验文件'},
         {id: '13', name: '工程监理资料'},
         {id: '14', name: '施工图设计文件'},
         {id: '15', name: '工程设计变更资料'},
         {id: '16', name: '竣工图纸'},
         {id: '17', name: '征地移民有关文件'},
         {id: '18', name: '重要会议记录'},
         {id: '19', name: '质量缺陷备案表'},
         {id: '20', name: '安全、质量事故资料'},
         {id: '21', name: '阶段验收鉴定书'},
         {id: '22', name: '竣工决算及审计资料'},
         {id: '23', name: '工程建设中使用的技术标准'},
         {id: '24', name: '工程建设标准强制性条文'},
         {id: '25', name: '专项验收有关文件'},
         {id: '26', name: '安全、技术鉴定报告'},
         {id: '27', name: '其他档案资料'}
       ];*/
      this.tableData = res.projectFiles;
      this.form.patchValue(res);
    });
  }

  handleChange({file, fileList}: UploadChangeParam): void {
    const status = file.status;
    this.uploadFileId = '';
    if (status !== 'uploading') {
      // console.log(file, fileList);
    }
    if (status === 'done') {
      this.uploadFileId = file.response.data.id;
    } else if (status === 'error') {

    }
  }

  openFileUploadModal(item) {
    this.fileUploadForm.patchValue(item);
    this.fileList = [];
    this.fileModalShow = true;
  }

  onConfirm() {
    if (!this.form.valid) {
      this.message.warning('信息填写有误');
      return;
    }
    const data = this.form.value;
    if (data.id) {
      this.fetchService.post('archives/wrsProjectInfo/edit', data).subscribe(res => {
        this.message.success('操作成功！');
        this.backToList();
      });
    } else {
      this.fetchService.post('archives/wrsProjectInfo/add', data).subscribe(res => {
        this.message.success('操作成功！');
        this.backToList();
      });
    }
  }

  onFileConfirm(): void {
    if (!this.uploadFileId) {
      this.message.error('请上传附件');
      return;
    }
    const param = {
      projectId: this.fileUploadForm.value.projectId,
      fileId: this.uploadFileId,
      num: this.fileUploadForm.value.num,
      version: this.fileUploadForm.value.version
    };
    this.fetchService.post('archives/wrsProjectFile/add', param).subscribe(res => {
      this.message.success('操作成功！');
      this.fileModalShow = false;
      this.checkDetail();
    });
  }

  onFileCancel(): void {
    this.fileModalShow = false;
  }


  backToList() {
    window.history.back();
  }

  versionManage() {
    this.router.navigate(['/project-files/history-versions']);
  }
}
